package com.ysp.summary.entity;

import cn.hutool.core.util.StrUtil;

import java.io.Serializable;

/**
 * 统一返回结果集
 * @author Administrator
 * @param <T>
 */
public class RespResult<T> implements Serializable {

    private static final long serialVersionUID = -1076279517660528766L;

    public static final String MSG = "SUCCESS";
    public static final String FAIL = "FAIL";

    private int code;
    private String msg;
    private T data;

    public RespResult(int code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public RespResult() {

    }

    public RespResult<T> setCode(int code){
        this.code = code;
        return this;
    }

    public RespResult<T> setMsg(String msg){
        this.msg = msg;
        return this;
    }

    public T getData(){
        return data;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public RespResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 返回成功
     */
    public static <T> RespResult<T> success(){
        return new RespResult<T>().setMsg(MSG);
    }

    public static <T> RespResult<T> success(T data){
        return new RespResult<T>().setMsg(MSG).setData(data);
    }

    public static <T> RespResult<T> fail(){
        return new RespResult<T>().setCode(-1).setMsg(FAIL);
    }

    public static <T> RespResult<T> fail(String msg){
        return new RespResult<T>().setCode(-1).setMsg(msg);
    }

    public static <T> RespResult<T> fail(Exception e){
        String msg = StrUtil.isBlank(e.getMessage()) ? FAIL : e.getMessage();
        return new RespResult<T>().setCode(-1).setMsg(msg);
    }
}
